<template>
	<view class="content">
		<view class="account">
			<i class="iconfont iconzhanghaoxinxi"></i>
			<input type="number" class="input_box" placeholder="请输入手机号" v-model="phone" name="phone" placeholder-style="color:#999999">
		</view>
		<view class="account">
			<i class="iconfont iconyanzhengma"></i>
			<input class="input_box" type="text" placeholder="请输入验证码" v-model="code" placeholder-style="color:#999999">
			<view type="default" class="button" :class="isGet ?'isTrue':''" @click="getCode">{{codeText}}</view>
		</view>
		<view class="account">
			<i class="iconfont iconmima1"></i>
			<input class="input_box" type="text" placeholder="请输入密码" v-model="password" name="password" placeholder-style="color:#999999">
		</view>
		<view class="tips"><text>*</text>最少8位，至少包括一个大写字母、一个小写字母、一个数字</view>
		<view class="account">
			<i class="iconfont iconmima1"></i>
			<input class="input_box" type="text" placeholder="请再次确认密码" v-model="cpassword" name="cpassword" placeholder-style="color:#999999">
		</view>
		<view class="agreerule">
			<view class="agreerule_box" @tap="switchArrow">
				<checkbox class="check" :checked="showArrow"/>
				<view>我已阅读并同意准守</view>
			</view>
			<view class="rule_link" @click="nav_toClause()">《智助教育服务条款》</view>
		</view>
		<text  @click="check_register" class="btn">注册</text>
	</view>
</template>

<script>
	import ajax from '@/common/request.js'
	import Checker from '@/common/graceChecker.js';
	export default {
		data() {
			return {
				showArrow: true,
				phone:"",
				password:"",
				cpassword:"",
				isGet: false, //判断是否获取验证码
				code: "" ,//验证码
				codeText: '获取验证码',
				count:0,
				timer:null
			};
		},
		onLoad() {
			
		},
		methods:{
			switchArrow() {
				this.showArrow = !this.showArrow
			},		
			/* 判断是否发送验证码 */
			getCode() {
				if(this.phone != "") {
					var yanData = {
						phone: this.phone,
						code_type: 'register_code'
					}
					this.$zapi.apiPost('user/currency.Code/SendCode',yanData).then(res => {
						if (res.data.code == 200) {
							this.getInterval(); //定时器
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					})
				} else {
					uni.showToast({
						title: '请输入手机号',
						icon:"none"
					})
				}
			},
			check_register(){
				//处理注册
				//验证是否阅读同意条款
				if(!this.showArrow){
					uni.showToast({
						title: '请先阅读服务条款并同意准守',
						icon:"none",
						mask: false,
						duration: 1500
					});
					return;
				}
				//判断规则
				var rule = [
					{
						name: "phone",
						checkType: "phoneno",
						checkRule: this.phone,
						errorMsg: "请输入正确的手机号"
					},
					{
						name: "pwd",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "密码不能为空"
					},
					{
						name: "format",
						checkType: "pwdFormat",
						checkRule: this.password,
						errorMsg: "密码格式不正确"
					},
					{
						name: "cpassword",
						checkType: "same",
						checkRule: this.password,
						errorMsg: "您两次输入的密码不相等"
					}
				];
				var data={'phone':this.phone,'pwd':this.password,'cpassword':this.cpassword,'format':this.password};
				var getCheck = Checker.check(data,rule);
				if(!getCheck){
					uni.showToast({
						title: Checker.error,
						mask: false,
						icon:"none",
						duration: 1500
					});
					return;
				}
				if(this.code != "") {
					//进行注册
					var datas = {
						'phone':this.phone,
						'pwd': this.password,
						'register_code': this.code,
						'register_type': 3
					};
					this.$zapi.apiPost('user/currency.user/Register',datas).then(res=>{
						if(res.data.code==200){
							uni.showToast({
								title: '注册成功',
								icon: "none",
								duration: 1500
							});
							setTimeout(function () {
								uni.redirectTo({
									url:'login'
								})
							}, 3000);
						} else if (res.data.code==201) {
							uni.showToast({
								title: '用户已注册',
								icon: "none",
								duration: 1500
							});
							setTimeout(function () {
								uni.navigateBack({
									delta:1
								})
							}, 3000);
						} else{
							uni.showToast({
								title:res.data.msg,
								icon: "none",
								duration:1500
							})
						}
					});	
				} else if(this.code == "") {
					uni.showToast({
						title: '请输入验证码',
						icon: "none",
						duration: 1500
					});
				}
			},
			nav_toClause(){
				uni.navigateTo({
					url:'clause'
				})
			},
			getInterval() {
				var that=this;
				uni.showToast({
					title: '短信发送成功',
					duration: 1000,
					success: function(res) {
						const TIME_COUNT = 60;
						that.count = TIME_COUNT;
						that.timer = setInterval(() => {
							if (that.count > 0 && that.count <= TIME_COUNT) {
								// 倒计时时不可点击
								that.isGet = true;
								// 计时秒数
								that.count--;
								// 更新按钮的文字内容
								that.codeText = that.count + 's后重新获取';
								console.log(that.codeText,"==");
							} else {
								// 倒计时完，可点击
								that.isGet = false;
								// 更新按钮文字内容
								that.codeText = '获取验证码';
								// 清空定时器!!!
								clearInterval(that.timer);
								that.timer = null;
							}
						}, 1000);
					}
				});
			}
		}
	}
</script>

<style lang="stylus" scoped>
	.content
		padding-top .4rem
		.account
			display flex
			align-items center
			border-bottom 1px solid #e5e5e5
			height 110upx
			width: 700upx
			margin: auto
			.i_size
				font-size 70upx
				color #999
			.input_box
				font-size .8rem
				margin-left 10upx
				width 600upx
		.agreerule
			display flex
			color #adadad
			font-size .6rem
			width: 700upx
			margin: .5rem auto 0
			.agreerule_box
				display flex
				align-items center
				.agreerule_box_btn
					display flex
					justify-content center
					align-items center
					width .6rem
					height .6rem
					margin-right .2rem
					border 1px solid #999999
					.i_agree
						font-size .8rem
			.rule_link
				color #00adff
				line-height 64upx
		.btn
			display flex
			padding .4rem 0
			justify-content center
			background-color #FF9900
			border-radius 5px
			box-shadow 0 .1rem .1rem rgba(0, 0, 0, .3)
			color #ffffff
			font-size 33upx
			width: 700upx
			margin: 1.3rem auto 0
		
</style>
<style>
	.iconfont{
		color: #FF9900;
		font-size: 50upx;
		margin-right: 10upx;
	}
	.check {
		height: 50upx;
		line-height: 50upx;
		transform: scale(0.5);
	}
	.check .wx-checkbox-input {
		margin-right: 10upx;
		-webkit-appearance: none;
		appearance: none;
		outline: 0;
		border: 2upx solid #999999;
		background-color: #FFFFFF;
		border-radius: 6upx;
		width: 22px;
		height: 22px;
		position: relative;
	}
	
	.check .wx-checkbox-input.wx-checkbox-input-checked {
		background-color: #FFFFFF;
		color: #666666 !important;
	}
	
	.check .wx-checkbox-input.wx-checkbox-input-checked:before{
		font-size: 60upx;
	}
	.button{
		white-space: nowrap;
		padding: 10upx 18upx;
		border: 2upx solid #E5E5E5;
		border-radius: 34upx;
	}
	.isTrue{
		color: #FFFFFF;
		background-color: #FF9900;
		border: 2upx solid #FF9900;
	}
	.tips{
		color: #CCCCCC;
		margin-right: 30upx;
		margin-left: 60upx;
	}
	.tips text {
		color: #FF7171;
		display: inline-block;
	}
</style>